package gov.nist.applet.phone.ua.gui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.MouseInputListener;

import com.dtec.swing.RoundButton;

/**
 * 
 * @author bluishoul@gmail.com
 * 
 */
public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private int locationY = 0;
	
	MainFrame main=null;

	/*---------------Menu----------------*/
	private JMenuBar MainMenuBar = null;

	/*-------------Basic Menu--------------*/
	private JMenu jmBasic = null;
	private JMenuItem jmiNewAccount = null;
	private JMenuItem jmiAddContact = null;
	private JMenuItem jmiExit = null;

	/*-------------Tools Menu--------------*/
	private JMenu jmTools = null;
	private JMenuItem jmiSettings = null;

	/*-------------Help Menu--------------*/
	private JMenu jmHelp = null;

	/*-------------About Menu--------------*/
	private JMenu jmAbout = null;

	/*-------------Panel--------------*/
	private JPanel jpUserStatus = null;
	private JButton jbPersonPhoto = null;
	private JLabel jlAccountInfo = null;
	private JLabel jlStatusTitle = null;
	private JPopupMenu jpmStatus = null;
	private JMenuItem jmiFree = null;

	private JPanel jpDial = null;
	private JTextField jtfContactAddr = null;
	private JButton jbDelete = null;
	private JButton jbAudioCall = null;
	private JButton jbVideoCall = null;

	private JPanel jpContact = null;
	private JPanel jpDialPanel = null;
	private JButton[] dialBtns = null;

	/**
	 * This is the default constructor
	 */
	public MainFrame() {
		super();
		main=this;
		initialize();
	}

	/**
	 * This method initializes the MainFrame
	 * 
	 * @return void
	 */
	private void initialize() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception exe) {
			exe.printStackTrace();
		}

		this.setSize(302, 650);

		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		int frameH = this.getHeight();
		int frameW = this.getWidth();
		this.setLocation((screenWidth - frameW) / 2,
				(screenHeight - frameH) / 2);

		this.setJMenuBar(getMainMenuBar());

		this.setLayout(null);

		this.add(getUserStatus());

		this.add(getDial());

		this.add(getContact());

		this.add(getDialPanel());

		this.setTitle("Sip Phone");
		this.setResizable(false);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	/*---------------Menu Setting--------------*/

	private JMenuBar getMainMenuBar() {
		MainMenuBar = new JMenuBar();

		// 基本菜单
		jmBasic = new JMenu("基本");
		jmiNewAccount = new JMenuItem("新建账户");
		jmiNewAccount.setIcon(new ImageIcon("images/menu/addAccountMenu.png"));
		jmiAddContact = new JMenuItem("添加联系人");
		jmiAddContact.setIcon(new ImageIcon("images/menu/addContact.png"));
		jmiExit = new JMenuItem("退出");
		jmiExit.setIcon(new ImageIcon("images/menu/quit.png"));
		jmBasic.add(jmiNewAccount);
		jmBasic.addSeparator();
		jmBasic.add(jmiAddContact);
		jmBasic.addSeparator();
		jmBasic.add(jmiExit);

		// 工具菜单
		jmTools = new JMenu("工具");
		jmiSettings = new JMenuItem("设置");
		jmiSettings.setIcon(new ImageIcon("images/menu/configureIcon.png"));
		jmTools.add(jmiSettings);
		MainMenuBar.add(jmBasic);
		MainMenuBar.add(jmTools);

		// 帮助
		jmHelp = new JMenu("帮助");
		MainMenuBar.add(jmHelp);

		// 关于
		jmAbout = new JMenu("关于");
		MainMenuBar.add(jmAbout);
		
		// MenuItem事件注册
		
		jmiNewAccount.addActionListener(menuItemListener);
		
		return MainMenuBar;
	}

	/*---------------Panel Setting--------------*/

	private JPanel getUserStatus() {
		jpUserStatus = new JPanel();
		jpUserStatus.setSize(294, 60);
		jpUserStatus.setLocation(0, locationY);
		jpUserStatus.setBackground(Color.WHITE);
		locationY += jpUserStatus.getHeight();
		jpUserStatus.setLayout(null);
		// Core Design
		// Left
		jbPersonPhoto = new JButton();
		jbPersonPhoto.setSize(50, 50);
		jbPersonPhoto
				.setIcon(new ImageIcon("images/userstatus/personPhoto.png"));
		jbPersonPhoto.setLocation(5, 5);
		jbPersonPhoto.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jpUserStatus.add(jbPersonPhoto);
		// Center
		jlAccountInfo = new JLabel("No Account", new ImageIcon(
				"images/userstatus/account.png"), JLabel.LEFT);
		jlAccountInfo.setSize(130, 50);
		jlAccountInfo.setLocation(80, 5);
		jpUserStatus.add(jlAccountInfo);
		// Right
		jlStatusTitle = new JLabel("当前状态", new ImageIcon(
				"images/menu/configureIcon.png"), JLabel.LEFT);
		jpmStatus = new JPopupMenu("------");
		jmiFree = new JMenuItem("在线");
		jmiFree.setIcon(new ImageIcon("images/userstatus/online.png"));
		jpmStatus.add(jmiFree);
		jlStatusTitle.setComponentPopupMenu(jpmStatus);
		jlStatusTitle.setSize(90, 20);
		jlStatusTitle.setLocation(200, 17);
		jlStatusTitle.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jlStatusTitle.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				if (event.getModifiers() == InputEvent.BUTTON1_MASK) {
					jpmStatus.show(event.getComponent(), 2, 19);
				}
			}

			public void mouseEntered(MouseEvent event) {
				jlStatusTitle.setForeground(Color.BLUE);
			}

			public void mouseExited(MouseEvent event) {
				jlStatusTitle.setForeground(null);
			}
		});

		jpUserStatus.add(jlStatusTitle);
		return jpUserStatus;
	}

	private JPanel getDial() {
		jpDial = new JPanel() {
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				ImageIcon img = new ImageIcon("images/dial/input1.png");
				g.drawImage(img.getImage(), 0, 0, null);
			};
		};
		jpDial.setLayout(null);
		jpDial.setSize(295, 52);
		jpDial.setLocation(0, locationY);
		jpDial.setBackground(Color.WHITE);
		locationY += jpDial.getHeight();

		// Core Design
		// Left
		jtfContactAddr = new JTextField(20);
		jtfContactAddr.setSize(130, 25);
		jtfContactAddr.setLocation(50, 13);
		jbDelete = new RoundButton("", 155, 180, 200);
		jbDelete.setIcon(new ImageIcon("images/dial/delete.png"));
		jbDelete.setSize(16, 16);
		jbDelete.setLocation(188, 17);
		jbDelete.setOpaque(false);
		jbDelete.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				String ca=jtfContactAddr.getText();
				if(ca.length()>0){
					jtfContactAddr.setText(ca.substring(0, ca.length()-1));
				}
			}
		});

		jpDial.add(jbDelete);

		// Right
		jbAudioCall = new JButton(new ImageIcon("images/dial/audio.png"));
		// jbAudioCall=new RoundButton("",155,180,200);
		// jbAudioCall.setIcon(new ImageIcon("images/dial/audio.png"));
		jbAudioCall.setSize(32, 32);
		jbAudioCall.setLocation(225, 9);
		jbAudioCall.addMouseListener(new CallButtonMouseClickAdapter());
		jpDial.add(jtfContactAddr);

		jbVideoCall = new JButton(new ImageIcon("images/dial/video.png"));
		jbVideoCall.setSize(32, 32);
		jbVideoCall.addMouseListener(new CallButtonMouseClickAdapter());
		jbVideoCall.setLocation(259, 9);
		
		jpDial.add(jbAudioCall);
		jpDial.add(jbVideoCall);

		return jpDial;
	}

	private JPanel getContact() {
		jpContact = new JPanel();
		jpContact.setSize(295, 280);
		jpContact.setLocation(0, locationY);
		jpContact.setBackground(Color.WHITE);
		locationY += jpContact.getHeight();
		return jpContact;
	}

	private JPanel getDialPanel() {
		jpDialPanel = new JPanel();
		jpDialPanel.setSize(220, 170);
		jpDialPanel.setLocation(42, locationY + 10);

		// Core Design
		jpDialPanel.setLayout(new GridLayout(4, 3, 10, 10));
		dialBtns = new JButton[12];
		int width = jpDialPanel.getWidth();
		int height = jpDialPanel.getHeight();
		for (int i = 0; i < 12; i++) {
			dialBtns[i] = new JButton();
			dialBtns[i].setSize(80, 60);
			dialBtns[i].setName("db" + i);
			dialBtns[i].addMouseListener(new DialButtonMouseClickAdapter());
		}
		try {
			dialBtns[0].setIcon(new ImageIcon("images/dialPanel/zero.png"));
			dialBtns[1].setIcon(new ImageIcon("images/dialPanel/one.png"));
			dialBtns[2].setIcon(new ImageIcon("images/dialPanel/two.png"));
			dialBtns[3].setIcon(new ImageIcon("images/dialPanel/three.png"));
			dialBtns[4].setIcon(new ImageIcon("images/dialPanel/four.png"));
			dialBtns[5].setIcon(new ImageIcon("images/dialPanel/five.png"));
			dialBtns[6].setIcon(new ImageIcon("images/dialPanel/six.png"));
			dialBtns[7].setIcon(new ImageIcon("images/dialPanel/seven.png"));
			dialBtns[8].setIcon(new ImageIcon("images/dialPanel/eight.png"));
			dialBtns[9].setIcon(new ImageIcon("images/dialPanel/nine.png"));
			dialBtns[10].setIcon(new ImageIcon("images/dialPanel/star.png"));
			dialBtns[11].setIcon(new ImageIcon("images/dialPanel/diez.png"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (int i = 1; i <= 9; i++) {
			jpDialPanel.add(dialBtns[i]);
		}
		jpDialPanel.add(dialBtns[10]);
		jpDialPanel.add(dialBtns[0]);
		jpDialPanel.add(dialBtns[11]);

		// jpDialPanel.setBackground(Color.magenta);
		return jpDialPanel;
	}

	
	//Event Listener/Adapter
	
	class DialButtonMouseClickAdapter extends MouseAdapter {
		public void mouseClicked(MouseEvent event) {
			JButton jbt = (JButton) event.getSource();
			int index = Integer.parseInt(jbt.getName().substring(2, jbt.getName().length()));
			String contactAddr = jtfContactAddr.getText();
			if (index >= 0 && index <= 9) {
				jtfContactAddr.setText(contactAddr + index);
			} else if (index == 10) {
				jtfContactAddr.setText(contactAddr + "*");
			} else if (index == 11) {
				jtfContactAddr.setText(contactAddr + "#");
			}
		}
	}
	
	class CallButtonMouseClickAdapter extends MouseAdapter {
		public void mouseClicked(MouseEvent event) {
			JButton jbt = (JButton) event.getSource();
			if(jbt.equals(jbAudioCall)){
//				new ChatDialog(main, true);
			}else if(jbt.equals(jbVideoCall)){
//				new ChatDialog(main, false);
			}
		}
	}

	ActionListener menuItemListener = new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource().equals(jmiNewAccount)){
				new AddNewAccountDialog(main);
			}
		}
	};
	
	public static void main(String[] args) {
		new MainFrame().setVisible(true);
	}
}
